package com.rxyl.love.petdoctor.acticity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.inner.GeoPoint;
import com.baidu.mapapi.search.core.RouteLine;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.util.SharedUtil;

import baidumap.api.DrivingRouteOverlay;

public class RoutePlanActivity extends AppCompatActivity implements OnGetRoutePlanResultListener {

    private MapView mapView;
    private BaiduMap baiduMap;
    private RoutePlanSearch search;
    private RouteLine routeLine;
    private double mlatitude, mlongtitude, nlatitude, nlongtitude;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_plan);
        mapView = (MapView) findViewById(R.id.route_mapView);
        Intent intent = getIntent();

        nlatitude = Double.parseDouble(SharedUtil.getString("latitude"));
        nlongtitude = Double.parseDouble(SharedUtil.getString("longitude"));
        mlatitude = intent.getDoubleExtra("hospitalLatitude", nlatitude);
        mlongtitude = intent.getDoubleExtra("hospitalLongitude", nlongtitude);

        baiduMap = mapView.getMap();
        search = RoutePlanSearch.newInstance();
        search.setOnGetRoutePlanResultListener(this);

        start();
    }

    public void start() {
        baiduMap.clear();
        LatLng eLatLng = new LatLng(mlatitude, mlongtitude);
        LatLng sLatLng = new LatLng(nlatitude, nlongtitude);

        PlanNode stNode = PlanNode.withLocation(sLatLng);
        PlanNode edNode = PlanNode.withLocation(eLatLng);

        search.drivingSearch(new DrivingRoutePlanOption().from(stNode).to(edNode));
    }

    @Override
    protected void onDestroy() {
        search.destroy();
        mapView.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

    }

    @Override
    public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

    }

    @Override
    public void onGetDrivingRouteResult(DrivingRouteResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(RoutePlanActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            // 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
            // result.getSuggestAddrInfo()
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {

            routeLine = result.getRouteLines().get(0);
            DrivingRouteOverlay overlay = new DrivingRouteOverlay(baiduMap);
            baiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(result.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
        }
    }


}
